package org.dromara.article.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.dromara.article.domain.vo.ArticleQueryVo;
import org.dromara.article.domain.vo.ArticleVo;
import org.dromara.article.domain.vo.DisuseArticleParamVo;
import org.dromara.article.emnu.ArticleTypeConstants;
import org.dromara.article.service.ArticleService;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文章接口
 */
@Slf4j
@RequestMapping("/article")
@RestController
public class ArticleController extends BaseController {

    @Resource
    private ArticleService articleService;

    /**
     * 上传文章压缩包，暂时仅支持zip格式文件
     *
     * @param file
     * @param category
     * @return
     */
    @PostMapping("/batchUpload")
    public R<Void> batchUpload(@RequestParam("file") MultipartFile file, @RequestParam("category") String category) {
        if (null == category) {
            return R.fail("文章类型不能为空");
        }
        //转换文章类型名称
        String typeName = ArticleTypeConstants.getNameById(category);
        return articleService.uploadArticle(file, typeName);
    }

    /**
     * 查询文章列表
     *
     * @param queryVo
     * @param pageQuery
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo<ArticleVo> list(ArticleQueryVo queryVo, PageQuery pageQuery) {
        return articleService.getArticleList(queryVo, pageQuery);
    }

    /**
     * 下载文章
     *
     * @param ids
     */
//    @PostMapping("/downloadArticle")
    public void downloadArticle(List<String> ids, HttpServletResponse response) {
        articleService.downloadArticle(ids, response);
    }

    /**
     * 查询单篇文章的内容
     *
     * @param articleId
     * @return
     */
    @GetMapping("/copy/{articleId}")
    public R<String> copy(@PathVariable("articleId") String articleId) {
        return articleService.getContent(articleId);
    }

    /**
     * 领取文章
     *
     * @param articleId
     * @return
     */
    @PutMapping("/claim/{articleId}")
    public R<Void> claim(@PathVariable("articleId") String articleId) {
        return articleService.claim(articleId);
    }

    /**
     * 废弃文章接口，支持批量废弃
     * @param paramVo
     * @return
     */
    @PutMapping("/disuse")
    public R<Void> disuseArticle(@RequestBody DisuseArticleParamVo paramVo) {
        return articleService.disuse(paramVo.getDisReason(), paramVo.getArticleId());
    }

}
